#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<int> smallestK(vector<int>& arr, int k) {

        srand(time(NULL));

        qsort(arr, 0, arr.size() - 1, k);

        return { arr.begin(),arr.begin() + k };
    }

    void qsort(vector<int>& nums, int l, int r, int k)
    {
        if (l >= r)
            return;

        int key = GetRandom(nums, l, r);

        int i = l;
        int left = l - 1;
        int right = r + 1;
        while (i < right)
        {
            if (nums[i] < key)
            {
                swap(nums[++left], nums[i++]);
            }
            else if (nums[i] == key)
            {
                i++;
            }
            else {
                swap(nums[--right], nums[i]);
            }
        }

        int a = left - l + 1;
        int b = right - left - 1;
        if (a >= k)
        {
            qsort(nums, l, left, k);
        }
        else if (a + b >= k)
        {
            return;
        }
        else {
            qsort(nums, right, r, k - a - b);
        }
    }

    int GetRandom(vector<int>& nums, int left, int right)
    {
        return nums[rand() % (right - left + 1) + left];
    }
};

int main()
{
	return 0;
}